package com.zz.word;


import com.alibaba.fastjson.JSONObject;
import com.deepoove.poi.policy.DynamicTableRenderPolicy;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;

import java.util.List;

/**
 * 年度销售变更确认书
 */
public class SaleChangePolicy extends DynamicTableRenderPolicy {

    @Override
    public void render(XWPFTable table, Object data) throws Exception {

        if (null == data) return;
        SaleChangeTableData detailData = (SaleChangeTableData) data;

        JSONObject salesData = detailData.getSalesData();
        if (salesData.size() > 0) {

            // 第一行
            String name = salesData.getString("name");

            XWPFTableRow row1 = table.getRow(0);
            XWPFTableRow row2 = table.createRow();
            XWPFTableRow row3 = table.createRow();

            XWPFTableCell cell1 = row1.getCell(0);
            setCellStyle(cell1,name);
            XWPFTableCell cell11 = row2.getCell(0);
            setCellStyle(cell11,"变更前指标");
            XWPFTableCell cell1111 = row3.getCell(0);
            setCellStyle(cell1111,"变更后指标");

            table.addRow(row1, 0);
            List<JSONObject> list = salesData.getJSONArray("list").toJavaList(JSONObject.class);
            int beforeSum = 0;
            int afterSum = 0;
            for (JSONObject jsonObject : list) {
                XWPFTableCell cell = row1.createCell();
                setCellStyle(cell,jsonObject.getString("year"));

                XWPFTableCell cell2 = row2.createCell();
                Integer before = jsonObject.getInteger("before");
                beforeSum += before;
                setCellStyle(cell2,String.valueOf(before));

                XWPFTableCell cell3 = row3.createCell();
                Integer after = jsonObject.getInteger("after");
                afterSum += after;
                setCellStyle(cell3,String.valueOf(after));
            }
            XWPFTableCell cellSum1 = row1.createCell();
            setCellStyle(cellSum1,"合计");

            XWPFTableCell cellSum2 = row2.createCell();
            setCellStyle(cellSum2,String.valueOf(beforeSum));

            XWPFTableCell cellSum3 = row3.createCell();
            setCellStyle(cellSum3,String.valueOf(afterSum));

            // 删除前两行空白
            table.removeRow(0);

        }

        //XWPFTableRow row1 = table.getRow(0);
        //XWPFTableCell cell1 = row1.getCell(0);
        //setCellStyle(cell1,"撒大大硕大的111");
        //
        //XWPFTableRow row2 = table.createRow();
        //XWPFTableCell cell11 = row2.getCell(0);
        //setCellStyle(cell11,"变更前指标222");

    }

    private void setCellStyle(XWPFTableCell cell , String text) {
        // 文字居中
        CTTc cttc = cell.getCTTc();
        CTTcPr ctPr = cttc.addNewTcPr();
        ctPr.addNewVAlign().setVal(STVerticalJc.CENTER);
        cttc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER);
        cell.removeParagraph(0); // 删除原有段落
        XWPFParagraph paragraph = cell.addParagraph();
        XWPFRun run = paragraph.createRun();
        run.setText(text);
        run.setFontSize(12);
        run.setFontFamily("微软雅黑");
        //垂直居中
        paragraph.setVerticalAlignment(TextAlignment.CENTER);
        //水平居中
        paragraph.setAlignment(ParagraphAlignment.CENTER);
    }
}